using System.Collections;
using UnityEngine;

public class Joggle : MonoBehaviour
{
    private WaitForSeconds waitTime = new(0.05f);

    public float speed;
    public float force;

    private float startTime = 0;
    //private float 
    // Start is called before the first frame update
    void Start()
    {
    }

    private void OnEnable()
    {
        startTime = Time.time;
        StartCoroutine(Do2());
    }

    private float Furmula(float t)
    {
        return Mathf.Sin(t * 0.5f) / Mathf.Max(t, 3.14f);
    }

    private void SetRotate(float value)
    {
        var pos = transform.localRotation.eulerAngles;
        pos.z = value;
        transform.localRotation = Quaternion.Euler(pos);
    }

    IEnumerator Do1()
    {
        while (true)
        {
            yield return waitTime;

            var time = Time.time - startTime;

            if (time > 2)
            {
                SetRotate(0);
                break;
            }

            float value = Furmula(time * speed) * force;
            SetRotate(value);
        }
    }

    private float Furmula2(float A, float W, float t)
    {
        float s1 = Mathf.Sin(W * t);
        float e1 = A * Mathf.Exp(-2 * t);
        return s1 * e1;
    }

    IEnumerator Do2()
    {
        while (true)
        {
            yield return waitTime;
            var time = Time.time - startTime;
            if (time > 1.5f)
            {
                SetRotate(0);
                break;
            }

            float value = Furmula2(3, 5, time * speed) * force;
            SetRotate(value);
        }
    }
}
